package com.shuguang.medicalcare;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.ref.WeakReference;

import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MedicalCareApi {
//    public static String LoginPath ="http://wxwork.gzsg.cn/wxwork/callcenter/sgCallEnter";

    public static String newPath ="http://49.234.94.134:8081/ccapp/";

//      public static String newPath ="http://wxwork.gzsg.cn/wxwork/";



      public static void testing(Context context) throws PackageManager.NameNotFoundException {
            PackageManager pm =context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);

            String versionName = pi.versionName;
            int versioncode = pi.versionCode;

            Log.i("versionName版本名称", versionName);

            Log.i("versioncode版本号", String.valueOf(versioncode));

            (new Thread(){
                  @Override
                  public void run() {

                        //实例化  里面可能会出现异常，需要try  catch
                        OkHttpClient client = new OkHttpClient();

                        String path= MedicalCareApi.newPath+"callcenter/checkVersion";

                        Log.i("path::", path);
//            http://49.234.94.134:8081/ccapp/callcenter/checkVersion?type=6&now_version=1.0.0

//                        Request request = new Request.Builder().url(path).build();
                        Request.Builder reqBuild = new Request.Builder();
                        HttpUrl.Builder urlBuilder =HttpUrl.parse(path)
                                .newBuilder();
                        urlBuilder.addQueryParameter("type", "6");
                        urlBuilder.addQueryParameter("now_version",String.valueOf(versionName)  );

                        reqBuild.url(urlBuilder.build());
                        Request request = reqBuild.build();



                        try {

                              //Response是okhttp实例化，并且调用Request。执行后返回的结果
                              Response response = client.newCall(request).execute();
                              String s = response.body().string();//获取到的json数据


                              JSONObject jsonObject =  new JSONObject(s);//转成json
                              int code = jsonObject.getInt("code");
                              Log.i("jsonObject", String.valueOf(jsonObject.getInt("code")) );
                              if(code == 0){
//                                    无需更新
//                                    showDialog(context.getApplicationContext());
                                      mHandler mhandler=new mHandler();
                                      Message msg = new Message();
                                      mhandler.sendMessage(msg);
                              }

                              if(code == 1){
//                                    需要更新
//                                    showDialog(context.getApplicationContext());
                              }

                        } catch (Exception e) {

                              e.printStackTrace();
                        }




                  }
            }).start();


      }


      private static class mHandler extends Handler {

            @Override
            public void handleMessage(Message msg){
                  Log.i("去更新", "handleMessage: ");


            }

      }

      public static void showDialog(Context ctx) {

            AlertDialog.Builder customizeDialog =
                    new AlertDialog.Builder(ctx);
             View dialogView = LayoutInflater.from(ctx)
                    .inflate(R.layout.change_task_dialog,null);

            customizeDialog.setView(dialogView);
            customizeDialog.setCancelable(false);

            AlertDialog alertDialog =  customizeDialog.show();


      }



}
